package com.wisdom;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.wisdom.MenuFragment.MenuHandler;
import com.wisdom.MenuFragment.MyAdapter;
import com.wisdom.MenuFragment.ViewHolder;
import com.wisdom.http.HttpRequestUrl;
import com.wisdom.vo.Users;



import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class RightFragment extends Fragment
{

	public static final int LOGIN_RESULT = 3;
	
    private Activity activity;
    
    private List<Map<String, Object>> mData;
    
    ListView listView;
    MyAdapter adapter;
    
    RelativeLayout head;
    LinearLayout layoutMyFollow;    
    
    LinearLayout layoutMyCollect;      
	//tv_bar_more = (TextView) findViewById(R.id.tv_title_bar_more);
	//MyFollowActivity
    
    LocalActivityManager manager = null;
    
    View page;
    //TextView userTitle;
    RelativeLayout layout_news_main;
    
    RelativeLayout layout_title_bar;    
    TextView mainTitleTV;
	
    @Override
    public void onAttach(Activity activity) 
    {
            this.activity = activity;
            super.onAttach(activity);
    }
	
	
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //TextView textView = new TextView(getActivity());
        //textView.setBackgroundColor(0x4400ffff);
        //return textView;
    	
		manager = new LocalActivityManager(activity, true);
        manager.dispatchCreate(savedInstanceState);
    	
    	layout_news_main = (RelativeLayout) activity.findViewById(R.id.layout_news_main);
    	layout_title_bar = (RelativeLayout) activity.findViewById(R.id.layout_title_bar);
    	mainTitleTV = (TextView) activity.findViewById(R.id.mainTitleTV);
    	
    	
    	mData = getData();
    	
    	
        
    	View view = inflater.inflate(R.layout.right_frame, container, false);
    	
    	head = (RelativeLayout)view.findViewById(R.id.head);
    	layoutMyFollow = (LinearLayout)view.findViewById(R.id.layoutMyFollow);
    	layoutMyCollect = (LinearLayout)view.findViewById(R.id.layoutMyCollect);    	
    	
    	
    	head.setOnClickListener(onLoginClickListener);
    	layoutMyFollow.setOnClickListener(onMyFollowClickListener);
    	layoutMyCollect.setOnClickListener(onMyCollectClickListener);

    	
    	/*
    	userTitle = (TextView) view.findViewById(R.id.userTitle);
    	if (Users.user != null)
    	{
    		userTitle.setText(Users.user.getUserName());
    	}
    	else
    	{
    		userTitle.setText("登录帐号");
    	}
    	*/
    	
    	
    	
        
        listView = (ListView) view.findViewById(R.id.rightListView);
        //listView.setSelector(R.drawable.menu_sharp);  //取消list点击时一闪(闪一下)的问题

        adapter = new MyAdapter(activity);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(OnListViewItemClickListener);
        
        //adapter.setSelectItem(0);
        
        //listView.setOnItemClickListener(this);
        
        

        
        return view;
        
        
    }

    


    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        
        Thread menuThread = new Thread(new RightMenuHandler());
		menuThread.start();	
        
        
        
        //ListView tv_bar_news = (ListView) findViewById(R.id.listView1);
        
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
        System.out.println("RightFragment-->onCreate");
        
        /*
        ListView listView = (ListView) getActivity().findViewById(R.id.listView1);

        //生成动态数组，并且转载数据
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<30;i++)
        {
        	HashMap<String, String> map = new HashMap<String, String>();
        	map.put("ItemTitle", "This is Title.....");
        	map.put("ItemText", "This is text.....");
        	mylist.add(map);
        }
        //生成适配器，数组===》ListItem
        SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), //没什么解释
        		                                    mylist,//数据来源 
        		                                    R.layout.vlist,//ListItem的XML实现
        		                                    
        		                                    //动态数组与ListItem对应的子项        
        		                                    new String[] {"ItemTitle", "ItemText"}, 
        		                                    
        		                                    //ListItem的XML文件里面的两个TextView ID
        		                                    new int[] {R.id.ItemTitle,R.id.ItemText});
        //添加并且显示
        listView.setAdapter(mSchedule);
        
        
        */
        
    }
    
    
    
    
	private OnClickListener onLoginClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
			head.setBackgroundColor(0x999999);
			
			if(Users.user == null)
			{
				Intent intent = new Intent(activity, LoginActivity.class);
				activity.startActivityForResult(intent, LOGIN_RESULT);

				//return;
			}
			
			//Toast.makeText(activity, "xxxx", Toast.LENGTH_SHORT).show();
		}

    };
    
	private OnClickListener onMyFollowClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
			if(Users.user == null )
			{
				Intent intent = new Intent(activity, LoginActivity.class);
				activity.startActivityForResult(intent, LOGIN_RESULT);
				return;
				
			}

			
			layout_news_main.removeAllViewsInLayout();
			layout_news_main.removeAllViews();
			
			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

			layout_title_bar.setVisibility(View.GONE);
			mainTitleTV.setText("我的跟贴");

			Intent intent = new Intent(getActivity(), MyFollowActivity.class);
			//intent.putExtra("title", HttpRequestUrl.URL_NEWS_TOP);

			page = manager.startActivity("myfollow", intent).getDecorView();
			layout_news_main.addView(page, params);			

		}

    };    
    

    private OnClickListener onMyCollectClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			
			if(Users.user == null )
			{
				Intent intent = new Intent(activity, LoginActivity.class);
				activity.startActivityForResult(intent, LOGIN_RESULT);
				return;
				
			}

			
			layout_news_main.removeAllViewsInLayout();
			layout_news_main.removeAllViews();
			
			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

			layout_title_bar.setVisibility(View.GONE);
			mainTitleTV.setText("我的收藏");

			Intent intent = new Intent(getActivity(), MyCollectActivity.class);
			//intent.putExtra("title", HttpRequestUrl.URL_NEWS_TOP);

			page = manager.startActivity("mycollect", intent).getDecorView();
			layout_news_main.addView(page, params);			

		}

    };   
    
    private OnItemClickListener OnListViewItemClickListener = new OnItemClickListener() 
    {

    	@Override
    	public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
    	{

    		
			Message message = new Message();
			Bundle bundle = new Bundle();
			bundle.putInt("position", position);
			message.setData(bundle);
			rightMenuHandler.sendMessage(message);
    		
    		
			//listView.setSelector(R.drawable.follow_menu_selector);
			
    		
			adapter.setSelectItem(position);
    		adapter.notifyDataSetInvalidated();
    		

    	}

    };   
    
    
    
    
    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("title", "对小编表达爱慕");
        map.put("img", R.drawable.mylove);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("title", "检测更新");
        map.put("img", R.drawable.myupdate);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "系统设置");
        map.put("img", R.drawable.mysetting);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "离线下载");
        map.put("img", R.drawable.offlinedown);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("title", "关于");
        map.put("img", R.drawable.about);
        list.add(map);
         
        return list;
    }	

    
    public final class ViewHolder
    {
        public ImageView img;
        public TextView title;
        //public TextView info;
        //public Button viewBtn;
        
        
    } 
 
    public class MyAdapter extends BaseAdapter{
    	
		private int itemID;
		private LayoutInflater mInflater;
		private Context context;
	
	
		public MyAdapter(Context context){
			this.context = context;
			this.mInflater = LayoutInflater.from(context);
		}
	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}
	
	
	
		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}
	
	
	
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			
			
			return 0;
		}
	
	
	
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) 
		{
			
			itemID = position;
			ViewHolder holder = null;
			if (convertView == null)
			{
				holder = new ViewHolder();  
				convertView = mInflater.inflate(R.layout.rightmenulist, null);
				holder.img = (ImageView)convertView.findViewById(R.id.img);
				holder.title = (TextView)convertView.findViewById(R.id.title);
				//holder.info = (TextView)convertView.findViewById(R.id.info);
				//holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
				convertView.setTag(holder);
		
			}
			else 
			{
				holder = (ViewHolder)convertView.getTag();
	
			}
	
	
	
	
	
			holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
			holder.title.setText((String)mData.get(position).get("title"));
			//holder.info.setText((String)mData.get(position).get("info"));
			/*
			holder.viewBtn.setOnClickListener(new View.OnClickListener() {
	
				@Override
				public void onClick(View v) {
					//showInfo();
					
					Log.e("onItemClick :", "onClick " + position);
				}
	
			});
	*/
			//convertView.setBackgroundColor(0x336699);
			/*
			if (position == 0)
			{
				convertView.setBackgroundResource((Integer)mData.get(position).get("img"));  
			}
			else if (position == 1)
			{
				convertView.setBackgroundResource(R.drawable.slidebar);  
			}
*/
			
			
			
			if (position == selectItem ) 
			{
				//convertView.setBackgroundColor(Color.RED);
				
				convertView.setBackgroundResource(R.drawable.menu_sharp_clicked_5);

				//convertView.getBackground().setAlpha(64);
			} 
			else 
			{
				convertView.setBackgroundResource(R.drawable.menu_sharp);
				//convertView.setBackgroundColor(Color.TRANSPARENT);
			} 
			
			

			

			/*
			convertView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.e("onItemClick_____ :", "onClick " + position);
					
					
					Message message = new Message();
					Bundle bundle = new Bundle();
					bundle.putInt("position", position);
					message.setData(bundle);
					phoneAuthHandler.sendMessage(message);

				}
			});
	*/
	
	
			return convertView;
	
		}
		

		
		public void setSelectItem(int selectItem) 
		{
			this.selectItem = selectItem;
		}
		
		private int selectItem = -1;

	
	
	
	}
    
	class RightMenuHandler implements Runnable 
	{
		@Override
		public void run()
		{
						
				
	        /*
			// 通过调用handler来通知UI主线程更新UI,
			
				Message message = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("id", position);
				message.setData(bundle);
				rightMenuHandler.sendMessage(message);
			*/
		
		}
	}
    
	
	public Handler rightMenuHandler = new Handler() 
	{
		public void handleMessage(Message msg) 
		{
			int position = msg.getData().getInt("position");	
			


			if (position == 0)
			{
				//Toast.makeText(activity, "1111111111111", Toast.LENGTH_SHORT).show();
				
				Intent intent = new Intent(activity, ILoveEditorActivity.class);
				//intent.putExtra("content_url", 25+"");
				//intent.putExtra("count", 3+"");
				activity.startActivityForResult(intent, position);
				

			}
			else if (position == 1)
			{
				Toast.makeText(activity, "2222222222222", Toast.LENGTH_SHORT).show();	
				
			}
			else if (position == 2)
			{
				Toast.makeText(activity, "3333333333333333", Toast.LENGTH_SHORT).show();
			}
			else if (position == 3)
			{
				Toast.makeText(activity, "44444444444444", Toast.LENGTH_SHORT).show();	
				
			}
			else if (position == 4)
			{
				Toast.makeText(activity, "55555555555555555555", Toast.LENGTH_SHORT).show();		
				
			}
		}
		
	};
    

}
